package com.mt.api.modules.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.modules.domain.Role;
import com.mt.api.modules.domain.RoleMenu;
import com.mt.api.modules.domain.vo.RoleMenuDetailVo;
import com.mt.api.modules.manager.IRoleManager;
import com.mt.api.modules.manager.IRoleMenuManager;
import com.mt.api.modules.service.IRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: plover
 * @Date: 2022-04-30
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements IRoleService {

    private final IRoleManager roleManager;
    private final IRoleMenuManager roleMenuManager;


    @Override
    public WebResponse getRoleList() {
        LambdaQueryWrapper<Role> wrapper = Wrappers.lambdaQuery();
        List<Role> roleList = roleManager.list(wrapper);
        return WebResponseUtils.successList(roleList);
    }

    @Override
    public WebResponse<RoleMenuDetailVo> roleDetail(Integer id) {
        Role role = roleManager.getById(id);
        if (Objects.isNull(role)) {
            return WebResponseUtils.errorParam("角色不存在");
        }
        List<RoleMenu> roleMenuList = roleMenuManager.getByRoleId(id);
        RoleMenuDetailVo vo = new RoleMenuDetailVo();
        vo.setId(role.getId());
        vo.setName(role.getName());
        vo.setRemark(role.getRemark());
        vo.setType(role.getType());
        List<Integer> menuIdList = roleMenuList.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        vo.setMenuIdList(menuIdList);
        return WebResponseUtils.success(vo);
    }
}
